package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.scity.beens.MovieBannerBeen;
import com.example.scity.beens.MovieBeen;
import com.example.scity.beens.MovieTypeBeen;
import com.example.scity.beens.MzyyBeen;
import com.example.scity.beens.ShjfBannerBeen;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MovieActivity extends AppCompatActivity {
    View view;
    LinearLayout movieyjfklinear,movieyylinear,movieyplinear,moviegplinear;
    ListView listView;
    Banner moviebanner;
    List<String> imglist=new ArrayList<>();
    MovieTypeBeen movieTypeBeen;
    MovieBeen movieBeen;
    RadioGroup radioGroup;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    MovieListView();
                    break;
                case 1:
                    handlerbanner();
                    break;
                case 2:
                    DisplayMovieType();
                    break;
                case 3:
                    break;
            }
        }
    };

    private void MovieListView() {
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return movieBeen.rows.size();
            }

            @Override
            public Object getItem(int i) {
                return movieBeen.rows.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                view = LayoutInflater.from(MovieActivity.this).inflate(R.layout.item_movie, null);
                ImageView imageView;
                imageView = view.findViewById(R.id.item_movie_img);
                Glide.with(view).load(Configs.Host + movieBeen.rows.get(i).cover).into(imageView);
                TextView title;
                title = view.findViewById(R.id.item_movie_title);
                title.setText(movieBeen.rows.get(i).title);
                /*TextView user;
                user = view.findViewById(R.id.item_movie_user);
                user.setText(movieBeen.rows.get(i).userName);*/
                TextView content;
                content = view.findViewById(R.id.item_movie_content);
                content.setText(Html.fromHtml(movieBeen.rows.get(i).content));
                /*TextView pltime;
                pltime = view.findViewById(R.id.item_movie_pltime);
                pltime.setText(movieBeen.rows.get(i).commentDate);*/
                return view;
            }
        });
    }

    private void handlerbanner() {
        moviebanner.setImages(imglist);
        moviebanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object o, ImageView imageView) {
                Glide.with(context).load(o).into(imageView);
            }
        });
        moviebanner.setDelayTime(2000);
        moviebanner.start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie);
        moviebanner=findViewById(R.id.movies_banner);
        listView = findViewById(R.id.movie_listView);
        loadmoviebanner();
        LoadMovielist();
        loadmovieyjfklinear();
    }

    private void loadmovieyjfklinear() {
        movieyjfklinear=findViewById(R.id.movieyjfklinear);
        movieyjfklinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MovieActivity.this,MovieYjfkActivity.class));
            }
        });
    }
    private void LoadMovielist() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Configs.Host + APIInterface.MovieList)
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                movieBeen = new Gson().fromJson(response.body().string(), MovieBeen.class);
                handler.sendEmptyMessage(0);
            }
        });
    }
    private void DisplayMovieType() {
        for(int i = 0 ;i<movieTypeBeen.data.size();i++){
            switch (i){
                case 0:
                    ((RadioButton)view.findViewById(R.id.movie_ryypradioButton)).setText(movieTypeBeen.data.get(i).name);
                    break;
                case 1:
                    ((RadioButton)view.findViewById(R.id.movie_jjsyradioButton)).setText(movieTypeBeen.data.get(i).name);
                    break;
                case 2:
                    ((RadioButton)view.findViewById(R.id.movie_jptjradioButton)).setText(movieTypeBeen.data.get(i).name);
                    break;
                case 3:
                    ((RadioButton)view.findViewById(R.id.movie_ylzxradioButton)).setText(movieTypeBeen.data.get(i).name);
                    break;

                default:
                    return;
            }
        }
        radioGroup.check(R.id.shjf_xwradioButton);
    }
    private void loadmoviebanner() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.MovieBannerList).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                MovieBannerBeen json = new Gson().fromJson(response.body().string(), MovieBannerBeen.class);
                for (int i = 0;i<json.rows.size();i++){
                    imglist.add(Configs.Host+json.rows.get(i).advImg);
                }
                handler.sendEmptyMessage(1);
            }
        });
    }
}